package com.castudio.Service.Impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.castudio.Common.RespPageEntity;
import com.castudio.Entity.User;
import com.castudio.Mapper.UserMapper;
import com.castudio.Service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
//import com.github.pagehelper.PageHelper;
//import com.github.pagehelper.PageInfo;
import io.swagger.models.auth.In;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 小陈同学
 * @since 2022-12-14
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Value("${wechat.appid}")
    private String appid;

    @Value("${wechat.secret}")
    private String secret;
    @Autowired
    private UserMapper userMapper;

    @Transactional
    public RespPageEntity getAllUserByPage(Integer page, Integer size, String realname) {
        RespPageEntity pageEntity = new RespPageEntity();
        // 默认从0开始
        if (page != null && size != null) {
            page = (page - 1) * size;
        }
        // 获取当前也用户信息
        List<User> users = userMapper.getAllUserByPage(page, size, realname);
        pageEntity.setData(users);
        // 获取当前用户总量
        Long total = userMapper.getTotal(realname);
        pageEntity.setTotal(total);
        return pageEntity;
    }

    @Override
    public void updateUser(String fileUrl, String dormnum, String gender, String nickname,
                           String phone, String realname, String stunum, String openid) {
        userMapper.updateUser(fileUrl, dormnum, gender, nickname, phone, realname, stunum, openid);
    }

    @Override
    public String getOpenId(String js_code) {
        /*小程序用户登录
         * 1. 从小程序端获取code
         * 2. 根据url 进行一一对应替换
         * 3. 由于小程序端传来的数据为一段字符型的数据
         * 3. 使用JSON格式对小程序端的数据进行转换
         * 4. 获得openid
         * */
        RestTemplate client = new RestTemplate();
        String url = "https://api.weixin.qq.com/sns/jscode2session?appid={appid}&secret={secret}&js_code={js_code}&grant_type=authorization_code";
        HashMap<String, Object> map = new HashMap<>();
        map.put("appid", appid);
        map.put("secret", secret);
        map.put("js_code", js_code);
        try {
            ResponseEntity response = client.getForEntity(url, String.class, map);
            String data = (String) response.getBody();
            HashMap<String, String> dataMap = JSON.parseObject(data, HashMap.class);
            String openid = dataMap.get("openid");
            return openid;
        } catch (Exception e) {
            log.error(e.toString());
            return null;
        }
    }

    @Override
    public IPage<User> getPage(Integer currentPage, Integer PageSize, User user) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<User>();
        queryWrapper.like(Strings.isNotEmpty(user.getRealname()), User::getRealname, user.getRealname());
        IPage page = new Page(currentPage, PageSize);
        userMapper.selectPage(page, queryWrapper);
        return page;
    }

    @Override
    public IPage<User> getPage(Integer currentPage, Integer pageSize) {
        IPage page = new Page(currentPage, pageSize);
        userMapper.selectPage(page, null);
        return page;
    }

}
